package windowUnits;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Point;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.util.Calendar;
import java.util.Date;

import javaUtils.DateUtils;

import javax.swing.JButton;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.JTableHeader;

public class MyDatePicker extends JFrame {
	
	private JTextField setField;
	private Calendar cal = Calendar.getInstance();
	private final JPanel panel = new JPanel();
	private final JButton leftButton = new JButton();
	private final JButton rightButton = new JButton();
	private final Font font = new Font("Courier New", Font.BOLD, 10);
	private final JLabel monthYearLabel = new JLabel(DateUtils.getDateNow(DateUtils.DF_MM_YYYY));
	private MyTableModel tableModel = new MyTableModel();
	private final JTable table = new JTable(tableModel);	
	
	private final JPanel tablePanel = new JPanel();
	private final JButton leftMonthButton = new JButton();
	private final JButton rightMonthButton = new JButton();
	
	/**
	 * Launch the application
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			MyDatePicker frame = new MyDatePicker(null);
			frame.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the frame
	 */
	public MyDatePicker(JTextField setField) {
		super();
		this.setField = setField;
		setBounds(100, 100, 247, 200);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		try {
			jbInit();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		//
	}
	private void jbInit() throws Exception {
		getContentPane().setLayout(null);
		addWindowFocusListener(new ThisWindowFocusListener());
		getContentPane().setBackground(new Color(255,255,255));
		setUndecorated(true);
		setResizable(false);
		setAlwaysOnTop(true);
		
		getContentPane().add(panel);
		panel.setBorder(new LineBorder(new Color(170,170,170), 1, false));
		panel.setLayout(null);
		panel.setBounds(10, 10, 228, 23);
		
		panel.add(leftButton);
		leftButton.addActionListener(new LeftButtonActionListener());
		leftButton.setMargin(new Insets(0, 0, 0, 0));
		leftButton.setIconTextGap(0);
		leftButton.setFont(font);
		leftButton.setText("<<");
		leftButton.setBounds(1, 1, 18, 21);
		leftButton.setBackground(new Color(232,232,232));
		
		panel.add(rightButton);
		rightButton.addActionListener(new RightButtonActionListener());
		rightButton.setMargin(new Insets(0, 0, 0, 0));
		rightButton.setFont(font);
		rightButton.setText(">>");
		rightButton.setBounds(209, 1, 18, 21);
		rightButton.setBackground(new Color(232,232,232));
		
		panel.add(monthYearLabel);
		monthYearLabel.setHorizontalAlignment(SwingConstants.CENTER);
		monthYearLabel.setForeground(new Color(255, 0, 0));
		monthYearLabel.setFont(new Font("Tahoma", Font.BOLD, 12));
		monthYearLabel.setHorizontalTextPosition(SwingConstants.CENTER);
		monthYearLabel.setBounds(45, 0, 136, 21);
		
		panel.add(leftMonthButton);
		leftMonthButton.addActionListener(new LeftMonthButtonActionListener());
		leftMonthButton.setMargin(new Insets(0, 0, 0, 0));
		leftMonthButton.setIconTextGap(0);
		leftMonthButton.setFont(new Font("Courier New", Font.BOLD, 10));
		leftMonthButton.setText("<");
		leftMonthButton.setBounds(19, 1, 18, 21);
		leftMonthButton.setBackground(new Color(232,232,232));
		
		panel.add(rightMonthButton);
		rightMonthButton.addActionListener(new RightMonthButtonActionListener());
		rightMonthButton.setMargin(new Insets(0, 0, 0, 0));
		rightMonthButton.setIconTextGap(0);
		rightMonthButton.setFont(new Font("Courier New", Font.BOLD, 10));
		rightMonthButton.setText(">");
		rightMonthButton.setBounds(191, 1, 18, 21);
		rightMonthButton.setBackground(new Color(232,232,232));
		
		getContentPane().add(tablePanel);
		tablePanel.setBorder(new LineBorder(new Color(232,232,232), 1, true));
		tablePanel.setLayout(new BorderLayout());
		tablePanel.setBounds(10, 39, 228, 151);
		
		tablePanel.add(table.getTableHeader(),BorderLayout.NORTH);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setRowHeight(20);
		table.setShowHorizontalLines(false);
		table.setShowVerticalLines(false);
		tablePanel.add(table,BorderLayout.CENTER);
		
		
		tableModel.setTableData(getTableData(cal));
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setRowSelectionAllowed(false);
		table.setEditingRow(6);
		table.setEditingColumn(7);
		
		for(int i=0;i<MyTableModel.columnIdentifiers.length;i++){
			table.getColumn(MyTableModel.columnIdentifiers[i]).setCellRenderer(new ButtonRenderer(cal));
			table.getColumn(MyTableModel.columnIdentifiers[i]).setCellEditor(new ButtonEditor(new JCheckBox(),setField,cal,this));
		}
		
	}
	private class ThisWindowFocusListener implements WindowFocusListener {
		public void windowGainedFocus(WindowEvent arg0) {
		}
		public void windowLostFocus(WindowEvent arg0) {
			this_windowLostFocus(arg0);
		}
	}
	private class LeftMonthButtonActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			leftMonthButton_actionPerformed(arg0);
		}
	}
	private class LeftButtonActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			leftButton_actionPerformed(arg0);
		}
	}
	private class RightMonthButtonActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			rightMonthButton_actionPerformed(e);
		}
	}
	private class RightButtonActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			rightButton_actionPerformed(e);
		}
	}
	
	private Object[][] getTableData(Calendar date){
		
		Calendar tableDate = Calendar.getInstance();
		tableDate.set(Integer.parseInt(DateUtils.getDateNow(DateUtils.DF_YYYY, date)), Integer.parseInt(DateUtils.getDateNow(DateUtils.DF_MM, date))-1, 1);
		Object[][] data = new Object[6][7];
		//date.set(Integer.parseInt(DateUtils.getDateNow(DateUtils.DF_YYYY, date)), Integer.parseInt(DateUtils.getDateNow(DateUtils.DF_MM, date)), 1);
		//date.set(Calendar.DAY_OF_MONTH, 1);
        int weekday = tableDate.get(Calendar.DAY_OF_WEEK);
		if(tableDate.getFirstDayOfWeek() == 2){
			if (weekday == 1){
				weekday=8;
			}
			weekday=weekday-1;
		}
		int currentMonth = Integer.parseInt(DateUtils.getDateNow(DateUtils.DF_MM, tableDate));
		int dayCount = 2;
        for(int i=0;i<6;i++){		
			Object[] weak = new Object[7];
			for(int j=0;j<7;j++){
				if(((j+1)>=weekday || i != 0) &&  currentMonth == Integer.parseInt(DateUtils.getDateNow(DateUtils.DF_MM, tableDate))){
					weak[j] = DateUtils.getDateNow(DateUtils.DF_DD, tableDate);
					tableDate.set(Calendar.DAY_OF_MONTH, dayCount);
					dayCount++;
				}else
					weak[j] = "";	
				
			}
			data[i]=weak;
		}
        		
		return data;
	}
	
	protected void this_windowLostFocus(WindowEvent arg0) {
		this.dispose();
	}
	protected void leftMonthButton_actionPerformed(ActionEvent arg0) {
		int month = Integer.parseInt(DateUtils.getDateNow(DateUtils.DF_MM, cal));
		cal.roll(Calendar.MONTH, false);
		if(month==1)
			cal.roll(Calendar.YEAR, false);
		monthYearLabel.setText(DateUtils.getDateNow(DateUtils.DF_MM_YYYY, cal));
		tableModel.setTableData(getTableData(cal));
		for(int i=0;i<MyTableModel.columnIdentifiers.length;i++){
			table.getColumn(MyTableModel.columnIdentifiers[i]).setCellRenderer(new ButtonRenderer(cal));
			table.getColumn(MyTableModel.columnIdentifiers[i]).setCellEditor(new ButtonEditor(new JCheckBox(),setField,cal,this));
		}
		//table.repaint();
	}
	protected void leftButton_actionPerformed(ActionEvent arg0) {
		cal.roll(Calendar.YEAR, false);
		monthYearLabel.setText(DateUtils.getDateNow(DateUtils.DF_MM_YYYY, cal));
		tableModel.setTableData(getTableData(cal));
		for(int i=0;i<MyTableModel.columnIdentifiers.length;i++){
			table.getColumn(MyTableModel.columnIdentifiers[i]).setCellRenderer(new ButtonRenderer(cal));
			table.getColumn(MyTableModel.columnIdentifiers[i]).setCellEditor(new ButtonEditor(new JCheckBox(),setField,cal,this));
		}
	}
	protected void rightMonthButton_actionPerformed(ActionEvent e) {
		int month = Integer.parseInt(DateUtils.getDateNow(DateUtils.DF_MM, cal));
		cal.roll(Calendar.MONTH, true);
		if(month==12)
			cal.roll(Calendar.YEAR, true);
		monthYearLabel.setText(DateUtils.getDateNow(DateUtils.DF_MM_YYYY, cal));
		tableModel.setTableData(getTableData(cal));
		for(int i=0;i<MyTableModel.columnIdentifiers.length;i++){
			table.getColumn(MyTableModel.columnIdentifiers[i]).setCellRenderer(new ButtonRenderer(cal));
			table.getColumn(MyTableModel.columnIdentifiers[i]).setCellEditor(new ButtonEditor(new JCheckBox(),setField,cal,this));
		}
	}
	protected void rightButton_actionPerformed(ActionEvent e) {
		cal.roll(Calendar.YEAR, true);
		monthYearLabel.setText(DateUtils.getDateNow(DateUtils.DF_MM_YYYY, cal));
		tableModel.setTableData(getTableData(cal));
		for(int i=0;i<MyTableModel.columnIdentifiers.length;i++){
			table.getColumn(MyTableModel.columnIdentifiers[i]).setCellRenderer(new ButtonRenderer(cal));
			table.getColumn(MyTableModel.columnIdentifiers[i]).setCellEditor(new ButtonEditor(new JCheckBox(),setField,cal,this));
		}
	}
}
